﻿Ext.define("PSI.User.ChangeUserPasswordForm", {
    createUI: function (entity) {
        var wnd = Ext.create("Ext.window.Window", {
            title: "修改密码",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 220,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 60,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            id: "editLoginName",
                            fieldLabel: "登录名",
                            value: entity.loginName,
                            xtype: "displayfield"
                        },
                        {
                            id: "editName",
                            fieldLabel: "姓名",
                            value: entity.name,
                            xtype: "displayfield"
                        },
                        {
                            id: "editPassword",
                            fieldLabel: "密码",
                            allowBlank: false,
                            blankText: "没有输入密码",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            inputType: "password",
                            name: "password"
                        },
                        {
                            id: "editConfirmPassword",
                            fieldLabel: "确认密码",
                            allowBlank: false,
                            blankText: "没有输入确认密码",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            inputType: "password"
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: this.onOK,
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                var me = this;
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: this
                        }
                    ]
                }
            ],

        });

        this.editPassword = Ext.getCmp("editPassword");
        this.editConfirmPassword = Ext.getCmp("editConfirmPassword");

        this.wnd = wnd;
    },

    show: function (entity) {
        this.createUI(entity);

        this.wnd.show();
    },

    // private
    onOK: function () {
        var me = this;
        var pass = this.editPassword.getValue();
        var pass2 = this.editConfirmPassword.getValue();
        if (pass != pass2) {
            PSI.MsgBox.showInfo("输入的密码和确认密码不一致，请重新输入", function () {
                me.editPassword.focus();
            });

            return;
        }

        var me = this;
        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/User/ChangePassword",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("成功修改密码", function () {
                    me.wnd.close();
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg);
            }
        });
    }

});